<template>
  <div class="CustomerContact">
    <el-form :inline="true" class="demo-form-inline">
      <div class="locationItem">
        <el-form-item label="客户编号">
          <el-input class="input-item" v-model="customer.code" disabled></el-input>
        </el-form-item>
        <el-form-item label="客户名称">
          <el-input class="input-item" v-model="customer.name" disabled></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 30px;" @click="">帮助</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 30px;" @click="onSubmit('新建')">新建</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 30px;" @click="$router.go(-1)">返回</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData.records" :highlight-current-row="true" style="max-height: 75%;width: 100%;">
      <el-table-column prop="id" :align="'center'" label="编号" width="120" />
      <el-table-column prop="name" label="姓名" width="120" />
      <el-table-column prop="gender" :align="'center'" label="性别" />
      <el-table-column prop="position" :align="'center'" label="职位" />
      <el-table-column prop="phone" :align="'center'" label="办公电话" />
      <el-table-column prop="telephone" :align="'center'" label="手机" />
      <el-table-column prop="content" :align="'center'" label="备注" />
      <el-table-column label="操作" :align="'center'">
        <template slot-scope="scope">
          <i class="el-icon-edit" @click="onSubmit('编辑',scope.$index, scope.row)"></i>
          <i class="el-icon-delete" @click="handleDelete(scope.$index, scope.row)"></i>
        </template>
      </el-table-column>
    </el-table>
    <div class="pager">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="current"
        :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total">
      </el-pagination>
    </div>
    <el-dialog :modal="true" :title="event+modName" :visible.sync="dialogFormVisible">
      <el-form :model="contact" :rules="rules" ref="contact" label-position="right" label-width="80px" :inline="true">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="contact.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="contact.gender">
            <el-radio :label="'男'">男</el-radio>
            <el-radio :label="'女'">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="contact.position"></el-input>
        </el-form-item>
        <el-form-item label="办公电话" prop="phone">
          <el-input v-model="contact.phone"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="telephone">
          <el-input v-model="contact.telephone"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="content">
          <el-input v-model="contact.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'CustomerContact',
    data() {
      return {
        modName: '联系人',
        event: '',
        currentIndex: '',
        currentRow: {},
        customer: {},
        contact: {},
        tableData: [],
        current: 1,
        pageSize: 5,
        dialogFormVisible: false,
        rowRemark: {},
        rules: {
          name: [{
            required: true,
            validator: this.$validator.checkName,
            message: '请输入姓名',
            trigger: 'blur'
          }],
          gender: [{
            required: true,
            message: '请选择性别',
            trigger: 'change'
          }],
          position: [{
            required: true,
            validator:this.$validator.checkMaxCharsNonNull,
            len:20,
            trigger: 'blur'
          }],
          phone: [{
            required: true,
            validator: this.$validator.checkPhone,
            trigger: 'blur'
          }],
          telephone: [{
            required: true,
            validator: this.$validator.checkTelePhone,
            trigger: 'blur'
          }],
          content: [{
            validator:this.$validator.checkMaxChars,
            len:25
          }]
        }
      }
    }, methods: {
      getRouterData() {
        this.customer = JSON.parse(sessionStorage.getItem('customer'));
      }, handleSizeChange(val) {
        this.pageSize = val;
        this.getContactList();
      }, handleCurrentChange(val) {
        this.current = val;
        this.getContactList();
      }, onSubmit(event, index, row) {
        this.event = event;
        // this.currentIndex = index;
        // this.currentRow = row;
        this.dialogFormVisible = true;
        this.contact = event == '编辑' ? row : {};
        console.log(this.currentIndex, this.currentRow);
      }, handleAdd() {
        this.contact.customerId = this.customer.id;
        this.sendeRequest('post', '/customer/customerInfo/contact/add', this.contact);
      }, handleEdit() {
        // let currentData = this.tableData.records[index];    //当前修改行数据
        // currentData = this.contact;
        this.sendeRequest('put', '/customer/customerInfo/contact/edit', this.contact);
      }, handleDelete(index, row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.tableData.records.splice(index, 1);
          this.tableData.total--;
          this.sendeRequest('delete', '/customer/customerInfo/contact/del', { id: row.id });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }, confirm() {
        this.$refs['contact'].validate((valid) => {
          if (valid) {
            switch (this.event) {
              case '新建':
                this.handleAdd();
                break;
              case '编辑':
                this.handleEdit();
                break;
            }
            this.cancel();
          } else {
            this.$message({
              message: '您还有未完善的选项',
              type: 'warning'
            })
            return false;
          }
        });
      }, cancel() {
        this.dialogFormVisible = false;
        this.tableData.records[this.currentIndex] = this.currentRow;
        console.log(this.currentIndex, this.currentRow);
        this.contact = {};
      }, getContactList() {
        let _this = this;
        _this.$http.get('/customer/customerInfo/contact', {
          params: {
            pageIndex: _this.current,
            pageSize: _this.pageSize,
            customerId: _this.customer.id
          }
        }).then(function (ret) {
          _this.tableData = ret.data;
        });
      }, sendeRequest(method, url, param) {  //实现更新请求的代码复用
        let _this = this;
        param = method == 'delete' ? { params: param } : param;
        this.$http[method](url, param).then(function (ret) {
          if (ret.data.code == 0) {
            _this.getContactList();
            _this.$notify({
              title: ret.data.msg,
              message: '😀',
              type: 'success'
            });
          } else {
            _this.$notify.error({
              title: ret.data.msg,
              message: '😭',
              type: 'error'
            });
          }
        });
      }
    }, created() {
      this.getRouterData(); //获取路由传递的参数
      this.getContactList(); //获取联系人数据
    }
  }
</script>

<style scoped>
  .CustomerContact {
    padding: 30px 30px;
    margin: 30px auto;
  }

  .locationButton {
    float: right;
    margin-top: -30px;
  }


  .input-item {
    width: 305.4px;
  }

  .el-form-item {
    margin: 0 0 22px 11px !important;
  }

  .pager {
    text-align: center;
    margin-top: 10px;
  }

  .el-input,
  .el-select {
    width: 260px;
  }
</style>